<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <h1>电商后台管理系统</h1>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="flag ? '64px' : '200px'">
        <div class="toggle-btn" @click="flag = !flag">|||</div>
        <el-menu
          :default-active="activePath"
          router
          text-color="#fff"
          background-color="#2c303b"
          unique-opened
          :collapse="flag"
          :collapse-transition="false"
          active-text-color="#00cc6a"
        >
          <!-- 一级导航 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="toggleNav('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menulist: [],
      // 图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      flag: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 从本地存储取值
    this.activePath = window.sessionStorage.getItem('navRoute')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()// 点击退出按钮清空token
      this.$router.push('/login')// 然后跳转到登录页
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    toggleNav (path) {
      this.activePath = path
      // 持久化，将path存入本地存储
      window.sessionStorage.setItem('navRoute', path)
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center; //拉伸
    background-color: #373d41;
    div {
      display: flex;
      align-items: center;
      color: #fff;
      margin-left: 10px;
      h1 {
        margin-left: 10px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    /* 折叠按钮 */
    .toggle-btn {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
    .el-menu {
      border-right: 1px solid #333744;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .iconfont {
    margin-right: 10px;
  }
}
</style>
